wayland: fix error handling for memfd_create
authorRay Strode <rstrode@redhat.com>
Mon, 16 May 2016 14:20:10 +0000 (10:20 -0400)
committerRay Strode <rstrode@redhat.com>
Thu, 16 Jun 2016 16:00:24 +0000 (12:00 -0400)
We currently use syscall() directly to invoke memfd_create,
since the function isn't available in libc headers yet.

The code, though, mishandles how errors are passed from syscall().
It assumes syscall returns the error code directly (but negative),
when in fact, syscall() uses errno.

Also, the code fails to retry on EINTR.

This commit moves the handling of memfd create to a helper function,
and changes the code to use errno and handle EINTR.

https://bugzilla.gnome.org/show_bug.cgi?id=766341

gdk/wayland/gdkdisplay-wayland.c

index c541a6a0b8454c76ae6443edd3b2ae889251e39d..9f78f298028443ab6b07907256ddee575517330c 100644 (file)
@@ -1104,6 +1104,23 @@ typedef struct _GdkWaylandCairoSurfaceData {
   uint32_t scale;
 } GdkWaylandCairoSurfaceData;
 
+static int
+open_shared_memory (void)
+{
+  int ret;
+
+  do
+    {
+      ret = syscall (__NR_memfd_create, "gdk-wayland", MFD_CLOEXEC);
+    }
+  while (ret < 0 && errno == EINTR);
+
+  if (ret < 0)
+    g_critical (G_STRLOC ": creating shared memory file failed: %m");
+
+  return ret;
+}
+
 static struct wl_shm_pool *
 create_shm_pool (struct wl_shm  *shm,
                  int             size,
@@ -1111,19 +1128,13 @@ create_shm_pool (struct wl_shm  *shm,
                  void          **data_out)
 {
   struct wl_shm_pool *pool;
-  int ret, fd;
+  int fd;
   void *data;
 
-  ret = syscall (__NR_memfd_create, "gdk-wayland", MFD_CLOEXEC);
-
-  if (ret < 0)
-    {
-      g_critical (G_STRLOC ": creating shared memory file failed: %s",
-                  g_strerror (-ret));
-      return NULL;
-    }
+  fd = open_shared_memory ();
 
-  fd = ret;
+  if (fd < 0)
+    return NULL;
 
   if (ftruncate (fd, size) < 0)
     {